<?php
namespace app\admin\model;

use think\Model;

class UserShop extends Base{
    protected $name = 'user_shop';

    // 获取用户列表
    public function getUserShopList($where = [], $curr = 1, $limit = 10, $field = '*', $order = 'id DESC'){
        $page = ($curr - 1) * $limit;

        $list = $this
            ->alias('s')
            ->join('users u', 's.user_id = u.id')
            ->where($where)
            ->field($field)
            ->limit($page, $limit)
            ->order($order)
            ->select();

        if($list->isEmpty()){
            return ['code' => 'fail'];
        }

        $starModel = new Star();
        $adminModel = new Admin();

        foreach($list as $k => &$v){
            $v['admin_name'] = $adminModel->getVal(['id' => $v['admin_id']], 'nickname')['data'];
            $star_name = $starModel->getVal(['id' => $v['star_id']], 'name');

            if(empty($star_name['data'])){
                $v['star_id'] = '会员';
            }else{
                $v['star_id'] = $star_name['data'];
            }
        }

        return ['code' => 'ok', 'list' => $list];
    }

    // 获取用户详情
    public function getUserShopShow($where = [], $field = '*'){
        $info = $this
            ->alias('s')
            ->join('users u', 's.user_id = u.id')
            ->where($where)
            ->field($field)
            ->find();

        if($info->isEmpty()){
            return ['code' => 'fail'];
        }

        $adminModel = new Admin();

        $info['admin_name'] = $adminModel->getVal(['id' => $info['admin_id']], 'nickname')['data'];

        return ['code' => 'ok', 'data' => $info];
    }

    /** 获取数量 */
    public function getUserShopNum($where = []){
        return $this->alias('s')->join('users u', 's.user_id = u.id')->where($where)->count();
    }
}